from datetime import datetime
import random

money = 0
j = 0
t_money = 0
while True:
    money = input("请输入您的银行卡余额:")
    if money.isdigit():
        money = int(money)
        money1 = money
        break
    else:
        print("余额非法, 重新输入!")

shopping_list = [["书", 123],
                 ["连衣裙", 500],
                 ["防晒霜", 234],
                 ["手机壳", 50],
                 ["相机", 4890],
                 ["平板", 3399],
                 ["手机", 6559]]
s_cart = []

# 购物车
for key, value in enumerate(shopping_list):
    print(key, value)

name = random.sample(shopping_list, 1)
name = name[0][0]
price = random.randint(1, 9)
print("恭喜您抽中", name, "的", price, "折优惠券")
print(name)

shopping_num_list = []
shoppinglist = []

while True:
    chose = input("请输入您想要商品的编号:")
    if chose.isdigit():
        chose = int(chose)

        if chose > len(shopping_list):
            print("对不起, 没这个商品!")
        else:
            if money > shopping_list[chose][1]:
                s_cart.append(shopping_list[chose])
                shopping_num = input('请输入想要的商品数量:')
                shopping_num = float(shopping_num)
                print(s_cart)

                if shopping_list[chose][0] == name:
                    money = money - shopping_list[chose][1] * shopping_num * price / 10
                else:
                    money = money - shopping_list[chose][1] * shopping_num
                print("恭喜, 添加", shopping_list[chose][0])
                shopping_num_list.append(shopping_num)
            else:
                print("余额不足, 请购买其他商品!")

    elif chose == "Q" or chose == "q":
        print("拜拜!")
        break
    elif chose == "T" or chose == "t":
        t_id = input("请输入退货商品编号:")
        t_id = int(t_id)
        if shopping_list[t_id] in s_cart:
            t_num = input("请输入退货数量:")
            t_num = int(t_num)
            it = s_cart.index(shopping_list[t_id])
            if shopping_num_list[it] > t_num:
                shopping_num_list[it] = shopping_num_list[it] - t_num
                if shopping_list[t_id][0] == name:
                    t_money = t_money - shopping_list[t_id][1] * shopping_num_list[it] * price / 10
            else:
                t_money = t_money - shopping_list[t_id][1] * shopping_num_list[it]
            money = money + t_money
            print("退货成功")
        elif shopping_num_list[it] == t_num:
            if shopping_list[t_id] == name:
                t_money = t_money - shopping_list[t_id][1] * shopping_num_list[it] * price / 10
            else:
                t_money = t_money - shopping_list[t_id][1] * shopping_num_list[it]
            money = money + t_money
            s_cart.remove(shoppinglist[t_id])
            print("退货成功")
        else:
            print("购物车中商品数量没有那么多!")
    else:
        print("输入非法,请重新输入!")

print("以下是您的购物小条, 请拿好!")
print("---------------------")
# 购买时间
dt = datetime.now()
print("购买时间: ", dt)
# 购物数量
while j < len(s_cart):
    print("购买物品: ", s_cart[j][0], "购买数量: ",int (shopping_num_list[j]))
    j = j + 1
    # 购买总商品的价格
    print("购买商品总价值", money1 - money)
    # 卡余额
    print("您的最终余额还剩:¥", money)



